package com.corner.ui.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.ConsTants;
import com.corner.app.GlobalParams;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.base.BaseActivity;
import com.corner.utils.BitmapUtil;
import com.corner.utils.FileUtil;
import com.corner.utils.view.AvaterCut;
import com.corner.utils.view.CustomFont;
import com.corner.utils.view.FindView;
import com.corner.utils.view.ImageCut;

import java.io.IOException;

/**
 * 选择图片显示的区域
 *
 * @author Administrator
 */
public class SelectPicShowRectActivity extends BaseActivity implements
        OnClickListener {
    public static final String SCALE = "Scale";
    public static final int OK = 1;
    public static final String PICTUREPATH = "picturePath";
    public static final int CANCEL = 2;
    public static final String INTENT_ID = "intent_id";
    public static final int AVATER = 1;
    public static final int IMAGE = 2;
    @FindView(R.id.image_cut_pic_show_select)
    private ImageCut ivCut;
    @FindView(R.id.avater_cut_pic_show_select)
    private AvaterCut avaterCut;
    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.tv_select_pic_rect_select)
    private TextView tvRectSelect;
    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.iv_select_pic_rect_scale)
    private ImageView ivScale;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.tv_select_pic_rect_cancel)
    private TextView tvCancle;
    private String bitmapPath;
    private int intent_id;

    @Override
    protected BasePresenter createP() {
        return null;
    }

    @Override
    protected void setOnClick() {
        tvCancle.setOnClickListener(this);
        tvRectSelect.setOnClickListener(this);
        ivScale.setOnClickListener(this);
        tvCancle.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);//下划线
        tvRectSelect.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);//下划线
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        intent_id = intent.getIntExtra(INTENT_ID, 0);
        bitmapPath = getIntent().getStringExtra(PICTUREPATH);
        switch (intent_id) {
            case AVATER:
                ivScale.setVisibility(View.INVISIBLE);
                avaterCut.setVisibility(View.VISIBLE);
                ivCut.setVisibility(View.GONE);
                avaterCut.setBitmapPath(bitmapPath);
                break;
            case IMAGE:
                ivCut.setBitmapPath(bitmapPath);
                break;
        }
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_select_pic_show);
    }

    @Override
    public void onClick(View v) {
        Intent intent = getIntent();
        switch (v.getId()) {
            case R.id.tv_select_pic_rect_select:
                try {
                    switch (intent_id) {
                        case AVATER:
                            String path = GlobalParams.APPFIEPATH + "/pic/" + bitmapPath.hashCode();
                            FileUtil.copyFile(bitmapPath, path);
                            intent.putExtra(SCALE, avaterCut.getScale());
                            intent.putExtra(PICTUREPATH, path);
                            break;
                        case IMAGE:
                            showProgressDialog();
                            path = GlobalParams.APPFIEPATH + "/pic/" + bitmapPath.hashCode();
                            BitmapUtil.compressBitmap(bitmapPath, path);
                            hideProgressDialog();
                            intent.putExtra(SCALE, ivCut.getScale());
                            intent.putExtra(PICTUREPATH, path);
                            break;
                    }
                    setResult(OK, intent);
                } catch (IOException e) {
                    e.printStackTrace();
                    setResult(CANCEL, intent);
                }
                finish();
                break;
            case R.id.tv_select_pic_rect_cancel:
                setResult(CANCEL, intent);
                finish();
                break;
            case R.id.iv_select_pic_rect_scale:
                ivCut.showAll();
                break;
        }
    }

}
